package com.hlhk.kmlparserforandroid;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.MapsInitializer;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.hlhk.kmlparserforandroid.kml.KMLReaderUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private AMap aMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MapsInitializer.updatePrivacyShow(this, true, true);
        MapsInitializer.updatePrivacyAgree(this, true);
        MapView moMap = findViewById(R.id.map);
        moMap.onCreate(savedInstanceState);
        aMap = moMap.getMap();
        aMap.setMapType(AMap.MAP_TYPE_SATELLITE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            if (!Environment.isExternalStorageManager()) {
                startActivity(new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION));
            }
        }
        getPermissions();
    }

    private void getPermissions() {
        XXPermissions.with(this)
                .permission(Permission.MANAGE_EXTERNAL_STORAGE)
                .permission(Permission.ACCESS_COARSE_LOCATION)
                .request(new OnPermissionCallback() {

                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {
                            AssetManager manager = getResources().getAssets();
                            try {
                                InputStream inputStream = manager.open("666.kml");
//                                InputStream inputStream = manager.open("sss.kml");
                                File f = asFile(inputStream);
                                new KMLReaderUtils().readKMLFile(aMap, f.getPath());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
                            XXPermissions.startPermissionActivity(MainActivity.this, permissions);
                        }
                    }
                });
    }

    public static File asFile(InputStream inputStream) throws IOException {
        File sdCard = Environment.getExternalStorageDirectory();
        String fileDir = sdCard.getPath() + File.separator + "tmp";
        File dir = new File(fileDir);
        if (!dir.exists()) {
            dir.mkdir();
        }
        File tmp = File.createTempFile("888", ".kml", new File(sdCard.getPath() + File.separator + "tmp"));
        OutputStream os = new FileOutputStream(tmp);
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        inputStream.close();
        return tmp;
    }
}